package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/11/8
 */
@Mapper
@Repository
public interface UserMapper {
    @Select("SELECT id,username,age,password FROM user WHERE username = #{username} AND password = #{password}")
    @Results(id = "User", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "password", column = "password"),
    })
    User selectUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Insert("INSERT INTO user (id, username, age, password) " +
            "VALUES (#{id}, #{username}, #{age}, #{password})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Long add(User user);

    @Select("SELECT id,username,age,password FROM user WHERE username = #{username}")
    @ResultMap("User")
    User findUserByName(String username);

    @Select("<script>" +
            "SELECT id,username,age,password FROM user WHERE 1=1" +
            "<if test='id != null'>" +
            " AND id=#{id}" +
            "</if>" +
            "<if test='username != null'>" +
            " AND username=#{username}" +
            "</if>" +
            "<if test='age != null'>" +
            " AND age=#{age}" +
            "</if>" +
            "<if test='password != null'>" +
            " AND password=#{password}" +
            "</if>" +
            "</script>")
    @ResultMap("User")
    List<User> findUserList(User user);
}
